#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
int n;
const int N=2005;
int t;
int a[N];
int dp[N][N];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n+1;i++){
            for(int j=1;j<=n+1;j++){
                dp[i][j]=0;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<i;j++){
                if(a[i+1]==a[i]){
                    dp[i+1][j]=max(dp[i+1][j],dp[i][j]+a[i]);
                    dp[j][i+1]=max(dp[j][i+1],dp[j][i]+a[i]);
                }else{
                    dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
                    dp[j][i+1]=max(dp[j][i+1],dp[j][i]);
                }
                if(a[i+1]==a[j]){
                    dp[i][i+1]=max(dp[i][i+1],dp[i][j]+a[j]);
                    dp[i+1][i]=max(dp[i+1][i],dp[j][i]+a[j]);
                }else{
                    dp[i][i+1]=max(dp[i][i+1],dp[i][j]);
                    dp[i+1][i]=max(dp[i+1][i],dp[j][i]);
                }
            }
        }
        int ans=0;
        for(int i=1;i<n;i++){
            ans=max(ans,max(dp[i][n],dp[n][i]));
        }
        printf("%d\n",ans);
    }
    return 0;
}